package com.corner.ui.activity;

import android.view.View;
import android.view.View.OnClickListener;

import com.corner.R;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.Info;
import com.corner.presenter.imp.ShowSinglePresenter;
import com.corner.ui.adapter.InfoAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.IShowSingleView;
import com.corner.utils.AppManager;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;
import com.corner.utils.view.widget.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/5/2.
 */
public class ShowSingleInfo extends BaseActivity<ShowSinglePresenter> implements OnClickListener, IShowSingleView {

    public static final String INFO_ID = "infoId";
    @FindView(R.id.lv_profile_info)
    private XListView lvInfos;
    private InfoAdapter infoAdapter;
    private MainActionBar actionBar;
    private List<Info> infos;
    private Long infoId;


    @Override
    protected ShowSinglePresenter createP() {
        return new ShowSinglePresenter();
    }

    @Override
    protected void setOnClick() {
        lvInfos.setPullLoadEnable(false);
        actionBar.btnLeft.setOnClickListener(this);
        lvInfos.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                presenter.requestInfo(infoId);
            }

            @Override
            public void onLoadMore() {
            }
        });
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnLeft.setVisibility(View.VISIBLE);
        actionBar.btnLeft.setText(getString(R.string.go_back));
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        infoId = getIntent().getLongExtra(INFO_ID, 0);
        presenter.requestInfo(infoId);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_show_single_info);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_actionbar_left:
                AppManager.getInstance().finishActivity(this);
                break;
        }
    }


    @Override
    public void showInfo(Info info) {
        if (info == null) {
            return;
        }
        if (infos == null) {
            infos = new ArrayList<Info>();
        }
        infos.clear();
        infos.add(info);
        if (infoAdapter == null) {
            infoAdapter = new InfoAdapter(infos, ShowSingleInfo.this, false);
            lvInfos.setAdapter(infoAdapter);
        } else {
            infoAdapter.changeList(infos);
        }
    }

    @Override
    public void requestInfoError(String message) {
        ToastUtil.showCustomToastInRelease(App.application, message);
    }

    @Override
    public void requestComplete() {
        lvInfos.stopRefresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (infoAdapter != null)
            infoAdapter.onDestroy();
    }
}
